package net.xxcxcxcx.xc_enhancedmob.skills.basics.individual.rare;

import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.entity.mob.MobEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.network.packet.s2c.play.EntityVelocityUpdateS2CPacket;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.math.Box;
import net.minecraft.util.math.Vec3d;
import net.xxcxcxcx.xc_enhancedmob.config.skill.SkillConfig;
import net.xxcxcxcx.xc_enhancedmob.skills.BaseSkill;
import net.xxcxcxcx.xc_enhancedmob.skills.SkillRarity;

import java.util.Iterator;
import java.util.List;
import java.util.Random;

public class PushSkill extends BaseSkill {

    public PushSkill(int level){super(level);}

    public PushSkill(NbtCompound nbtCompound){super(nbtCompound);}

    public PushSkill(){
    }

    @Override
    public SkillConfig getDefaultConfig() {
        SkillConfig skillConfig = super.getDefaultConfig();
        skillConfig.setMessage(getConfigMessage(skillConfig));
        return skillConfig;
    }

    @Override
    public String getConfigMessage(SkillConfig skillConfig) {
        if (skillConfig == null) return super.getConfigMessage(null);
        return "推斥技能，对附近所有生物造成伤害并击退";
    }

    public SkillRarity getSkillRarity() {
        return SkillRarity.Rare;
    }

    @Override
    public boolean canUseAbility(MobEntity mobEntity) {
        return (mobEntity.world.getTime()>getSkilltime());
    }

    @Override
    public boolean onAbilityUse(MobEntity mobEntity) {
        if (canUseAbility(mobEntity)) {
            setSkilltime(mobEntity.world.getTime() + getCooldown() - getSkillLevel());
            int L = getSkillLevel() * 4;
            double Range = (double)getSkillLevel() * 2.0d + 5.0d;
            Box Box = mobEntity.getBoundingBox().stretch(L, L / 2, L).stretch(-L, -L / 2, -L);
            List<Entity> entityList = mobEntity.world.getOtherEntities(mobEntity, Box);
            Iterator iterator1 = entityList.iterator();
            while (iterator1.hasNext()) {
                Entity target = (Entity) iterator1.next();
                double X = target.getX() - mobEntity.getX();
                double Y = target.getY() - mobEntity.getY();
                double Z = target.getZ() - mobEntity.getZ();
                double distence = Math.sqrt(X*X+Y*Y+Z*Z);
                if ((X * X + Y * Y + Z * Z) < Range * Range) {
                    target.damage(DamageSource.FALL, getSkillLevel()/(float)Range);
                    double Vx = getSkillLevel()*(X/distence);
                    double Vy = getSkillLevel()*(Y/distence);
                    double Vz = getSkillLevel()*(Z/distence);
                    target.setVelocity(3*Vx/Range,Vy/Range+0.2d,3*Vz/Range);
                    if(target instanceof ServerPlayerEntity playerEntity)
                    {
                        playerEntity.networkHandler.sendPacket(new EntityVelocityUpdateS2CPacket(target.getEntityId(), new Vec3d(3*Vx/Range, Vy/Range+0.2d, 3*Vz/Range)));
                    }
                }
            }
        }
        return true;
    }
}
